﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.vo;
using Business.exception;

namespace Business.dao
{
    internal class UserDao : AbstractDao
    {

        public User GetById(Int32 id)
        {
            User user = null;

            try
            {
                user = mapper.QueryForObject<User>("User.selectById", id);

            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                throw new WebSiteException("Error retrieving user from database", ex);
            }

            return user;
        }

        public User GetByLogin(String login)
        {
            User user = null;

            try
            {
                user = mapper.QueryForObject<User>("User.selectByLogin", login);

            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                throw new WebSiteException("Error retrieving user from database", ex);
            }

            return user;
        }       

        public IList<User> GetAll()
        {
            IList<User> users = null;

            try
            {
                users = mapper.QueryForList<User>("User.selectAll", null);
            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                throw new WebSiteException("Error retrieving users from database", ex);
            }

            return users;
        }

        public void Save(User user)
        {
            try
            {
                mapper.Insert("User.insert", user);
            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                throw new WebSiteException("Error retrieving users from database", ex);
            }
        }


        public void Update(User user)
        {
            try
            {
                mapper.Update("User.update", user);
            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                throw new WebSiteException("Error retrieving users from database", ex);
            }
        }

        public void Delete(Int32 id)
        {
            try
            {
                mapper.Delete("User.deleteById", id);
            }
            catch (System.Exception ex)
            {
                log.Error(ex);
                throw new WebSiteException("Error retrieving users from database", ex);
            }
        }
    }
}
